package com.anyi.gulimall.ware.service.impl;

import com.anyi.gulimall.common.to.SkuHasStockVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.anyi.gulimall.common.utils.PageUtils;
import com.anyi.gulimall.common.utils.Query;

import com.anyi.gulimall.ware.dao.WmsWareSkuDao;
import com.anyi.gulimall.ware.entity.WmsWareSkuEntity;
import com.anyi.gulimall.ware.service.WmsWareSkuService;
import org.springframework.util.StringUtils;


@Service("wmsWareSkuService")
public class WmsWareSkuServiceImpl extends ServiceImpl<WmsWareSkuDao, WmsWareSkuEntity> implements WmsWareSkuService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        /**
         *    wareId: 123,//仓库id
         *    skuId: 123//商品id
         */
        LambdaQueryWrapper<WmsWareSkuEntity> wrapper = new LambdaQueryWrapper<>();
        String  wareId = (String) params.get("wareId");

        String  skuId = (String) params.get("skuId");
        if (!StringUtils.isEmpty(wareId)){
            wrapper.eq(WmsWareSkuEntity::getWareId,Long.parseLong(wareId));
        }
        if (!StringUtils.isEmpty(skuId)){
            wrapper.eq(WmsWareSkuEntity::getSkuId,Long.parseLong(skuId));
        }
        IPage<WmsWareSkuEntity> page = this.page(
                new Query<WmsWareSkuEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }


    @Override
    public List<SkuHasStockVo> getSkuHasStock(List<Long> skuIds) {
        List<SkuHasStockVo> skuHasStockVos = skuIds.stream().map(item -> {
            Long count = this.baseMapper.getSkuStock(item);
            SkuHasStockVo skuHasStockVo = new SkuHasStockVo();
            skuHasStockVo.setSkuId(item);
            skuHasStockVo.setHasStock(count == null?false:count > 0);
            return skuHasStockVo;
        }).collect(Collectors.toList());
        return skuHasStockVos;
    }

}